//
//  LeftMenuViewController.m
//  Hive
//
//  Created by 惠俊贝 on 15/9/2.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "UserSetViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "LeftSlideViewController.h"
#import "ProviderInfoViewController.h"
#import "HelpViewController.h"
#import "AboutViewController.h"
#import "AlarmViewController.h"
#import "AccoutViewController.h"
#import "UserDefaultUtil.h"

@interface LeftMenuViewController () {
    NSArray *menuTitleArray;
    NSArray *imageNameArray;
    
    UINavigationController *rootNavVC;
    
    __weak IBOutlet UILabel *userNameLabel;
    __weak IBOutlet UILabel *userAccountLabel;
}

@end

@implementation LeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = [[UIScreen mainScreen] bounds];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    rootNavVC = appDelegate.navigationController;
    
    menuTitleArray = @[@"账户信息"
                       ,@"消息中心"
//                       ,@"帮助与反馈"
//                       ,@"好评"
                       ,@"关于"];
    
    imageNameArray = @[@"icon_server"
                       ,@"icon_msg"
                       ,@"icon_about"];
    
    userNameLabel.text = [UserDefaultUtil getUserName];
    userAccountLabel.text = [UserDefaultUtil getUserAccount];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - events
- (IBAction)accoutGestureTap:(id)sender {
    AccoutViewController *toController = [[AccoutViewController alloc] initWithNibName:@"AccoutViewController" bundle:nil];
    [rootNavVC pushViewController:toController animated:YES];
    
}

- (IBAction)userSetButtonPress:(id)sender {
    
    [self showOrHideLeftView];
    
    UserSetViewController *toController = [[UserSetViewController alloc] init];
    [rootNavVC pushViewController:toController animated:YES];
}

- (IBAction)logoutButtonPress:(id)sender {
    //TODO://跳出提示框
    UIAlertView *logoutAlert = [[UIAlertView alloc] initWithTitle:nil message:@"请注意，退出后将不能收到设备的报警信息，确定要退出？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    logoutAlert.tag = 110;
    [logoutAlert show];
}


- (void)logout {
    //TODO://删除用户信息token等
    UIViewController *toController;
    for (UIViewController *vc in rootNavVC.viewControllers) {
        if ([vc isKindOfClass:[LoginViewController class]]) {
            toController = vc;
            break;
        }
    }
    if (toController) {
        [rootNavVC popToViewController:toController animated:YES];
    }
    else {
        toController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        [rootNavVC pushViewController:toController animated:YES];
    }

}
- (void)showOrHideLeftView {
    for (id vc in rootNavVC.viewControllers) {
        if ([vc isKindOfClass:[LeftSlideViewController class]]) {
            LeftSlideViewController *LeftSlideVC = (LeftSlideViewController *)vc;
            if (LeftSlideVC.closed)
            {
                [LeftSlideVC openLeftView];
            }
            else
            {
                [LeftSlideVC closeLeftView];
            }
        }
    }
}

#pragma mark - UIAlertViewDelegate 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 110) {
        if (buttonIndex == 1) {
            //确定
            [self logout];
        }
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [menuTitleArray count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"cell" ;
    
    NSString *menuTitle = [menuTitleArray objectAtIndex:indexPath.row];
    NSString *imageName = [imageNameArray objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        [cell.imageView setImage:[UIImage imageNamed:imageName]];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:15.0];
        cell.textLabel.text = menuTitle;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    if (indexPath.section == 0) {
        
        if ([cell.textLabel.text isEqualToString:@"账户信息"]) {
            [self showOrHideLeftView];
            AccoutViewController *toController = [[AccoutViewController alloc] initWithNibName:@"AccoutViewController" bundle:nil];
            [rootNavVC pushViewController:toController animated:YES];
        }
        else if ([cell.textLabel.text isEqualToString:@"消息中心"]) {
            [self showOrHideLeftView];
            AlarmViewController *toController = [[AlarmViewController alloc] init];
            [rootNavVC pushViewController:toController animated:YES];
        }
        else if ([cell.textLabel.text isEqualToString:@"关于"]) {
            [self showOrHideLeftView];
            AboutViewController *toController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
            [rootNavVC pushViewController:toController animated:YES];
        }
        
        /*
        switch (indexPath.row) {
            case 0: {
                // 服务商信息
                [self showOrHideLeftView];
                ProviderInfoViewController *toController = [[ProviderInfoViewController alloc] init];
                [rootNavVC pushViewController:toController animated:YES];
                
                break;
            }
            case 1: {
                // 报警信息
                [self showOrHideLeftView];
                AlarmViewController *toController = [[AlarmViewController alloc] init];
                [rootNavVC pushViewController:toController animated:YES];
                break;
            }
            case 2: {
                // 帮助与反馈
                [self showOrHideLeftView];
                HelpViewController *toController = [[HelpViewController alloc] init];
                [rootNavVC pushViewController:toController animated:YES];
                break;
            }
            case 3: {
                // 好评
                NSString  * nsStringToOpen = CommentUrlStr;
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
                break;
            }
            case 4: {
                // 关于
                [self showOrHideLeftView];
                AboutViewController *toController = [[AboutViewController alloc] init];
                [rootNavVC pushViewController:toController animated:YES];
                break;
            }
                
            default: {
                break;
            }
        }
         */
    }
    
}
@end
